<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\SearchNoticeFormat;
use App\Http\Requests\CheckRequest;
use App\Http\Requests\SearchNoticeCreateRequest;
use App\Services\SearchNoticeService;
use Illuminate\Http\Request;

class SearchNoticeController extends Controller
{
    protected $searchNoticeService;

    public function __construct(SearchNoticeService $searchNoticeService)
    {
        $this->searchNoticeService = $searchNoticeService;
    }

    public function getLists(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('type')) {
            $search['type'] = $request->input('type');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }
        if ($request->input('title')) {
            $search['title'] = $request->input('title');
        }
        if ($request->input('order_column')) {
            $search['order_column'] = $request->input('order_column');
        }
        if ($request->input('order_type')) {
            $search['order_type'] = $request->input('order_type', 'desc');
        }
        $res = $this->searchNoticeService->getLists($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function getAdminList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('type')) {
            $search['type'] = $request->input('type');
        }
        if ($request->input('status')) {
            $search['status'] = $request->input('status');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }
        if ($request->input('title')) {
            $search['title'] = $request->input('title');
        }
        if ($request->input('order_column')) {
            $search['order_column'] = $request->input('order_column');
        }
        if ($request->input('order_type')) {
            $search['order_type'] = $request->input('order_type', 'desc');
        }
        if ($request->input('check_status')) {
            $search['check_status'] = $request->input('check_status');
        }
        $res = $this->searchNoticeService->getAdminList($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function getMySearchNotice(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $userId = $request->input('user_id');
        $search = [];
        if ($request->input('type')) {
            $search['type'] = $request->input('type');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }
        if ($request->input('title')) {
            $search['title'] = $request->input('title');
        }
        if ($request->input('order_column')) {
            $search['order_column'] = $request->input('order_column');
        }
        if ($request->input('order_type')) {
            $search['order_type'] = $request->input('order_type', 'desc');
        }
        if ($request->input('check_status')) {
            $search['check_status'] = $request->input('check_status');
        }
        $res = $this->searchNoticeService->getOwnLists($page, $pageSize, $userId, $search);
        return renderSuccessJson($res);
    }

    public function create(SearchNoticeCreateRequest $searchNoticeCreateRequest)
    {
        $format = new SearchNoticeFormat($searchNoticeCreateRequest->all());
        $this->searchNoticeService->create($format);
        return renderSuccessJson();
    }

    public function adminCreate(SearchNoticeCreateRequest $searchNoticeCreateRequest)
    {
        $format = new SearchNoticeFormat($searchNoticeCreateRequest->all());
        $this->searchNoticeService->adminCreate($format);
        return renderSuccessJson();
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少 id');
        }
        $detail = $this->searchNoticeService->getDetail($id);
        return renderSuccessJson($detail);
    }

    public function check(CheckRequest $checkRequest)
    {
        $id = $checkRequest->input('id');
        $checkStatus = $checkRequest->input('check_status');
        $checkMessage = $checkRequest->input('check_message') ?? '';
        $this->searchNoticeService->check($id, $checkStatus, $checkMessage);
        return renderSuccessJson();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        $this->searchNoticeService->delete($id);
        return renderSuccessJson();
    }
}
